const { ethers } = require('ethers');

async function createTokenWithC4pad() {
  try {
    // 私钥和网络配置
    const privateKey = '0x5139beaf880f2b922b14096f83b804d67b83e003ee93ffee59a9816f9cea448a';
    const rpcUrl = 'https://testrpc.xlayer.tech/terigon';
    
    // 创建provider和wallet
    const provider = new ethers.JsonRpcProvider(rpcUrl);
    const wallet = new ethers.Wallet(privateKey, provider);
    
    console.log('使用地址:', wallet.address);
    
    // 检查余额
    const balance = await provider.getBalance(wallet.address);
    console.log('当前余额:', ethers.formatEther(balance), 'OKB');
    
    // 从c4pad.fun获取的交易数据
    const transactionData = {
      to: "0xd9B7BDD6DbC80DaEc00fB0a6c920eA50C1eDF017",
      value: "10000000000000000", // 0.01 OKB
      data: "0xf5c60c1a00000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000002b5e3af16b1880000000000000000000000000000000000000000000000000000000000000000000854657374446f6765000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000554444f4745000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e41207465737420746f6b656e20666f722064656d6f6e7374726174696f6e0000000000000000000000000000000000000000000000000000000000000000001368747470733a2f2f6578616d706c652e636f6d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000001468747470733a2f2f742e6d652f6578616d706c65000000000000000000000000000000000000000000000000000000000000000000000000000000000000001568747470733a2f2f782e636f6d2f6578616d706c650000000000000000000000",
      gasLimit: "600000",
      gasPrice: "8333333333"
    };
    
    console.log('准备发送交易...');
    console.log('交易详情:');
    console.log('- 目标合约:', transactionData.to);
    console.log('- 发送金额:', ethers.formatEther(transactionData.value), 'OKB');
    console.log('- Gas 限制:', transactionData.gasLimit);
    console.log('- Gas 价格:', ethers.formatUnits(transactionData.gasPrice, 'gwei'), 'Gwei');
    
    // 发送交易
    const tx = await wallet.sendTransaction(transactionData);
    console.log('交易已发送!');
    console.log('交易哈希:', tx.hash);
    
    // 等待交易确认
    console.log('等待交易确认...');
    const receipt = await tx.wait();
    console.log('交易已确认!');
    console.log('区块号:', receipt.blockNumber);
    console.log('Gas 使用量:', receipt.gasUsed.toString());
    
    // 如果有事件日志，解析代币创建信息
    if (receipt.logs && receipt.logs.length > 0) {
      console.log('交易事件日志:');
      receipt.logs.forEach((log, index) => {
        console.log(`日志 ${index}:`, log);
      });
    }
    
    return receipt;
    
  } catch (error) {
    console.error('创建代币失败:', error);
    if (error.reason) {
      console.error('失败原因:', error.reason);
    }
    if (error.data) {
      console.error('错误数据:', error.data);
    }
  }
}

// 运行脚本
createTokenWithC4pad();
